package com.wuye.action;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wuye.common.util.CommonUtil;
import com.wuye.common.vo.ZTreeInfo;
import com.wuye.common.vo.PageInfo;
import com.wuye.entity.Organization;
import com.wuye.entity.User;
import com.wuye.services.BranchServiceManager;

public class BranchAction extends ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(BranchAction.class);
	
	@Setter
	@Getter
	private int page;
	@Setter
	@Getter
	private String inParma;
	@Setter
	@Getter
	private String rtnMsg;
	
	@Autowired
	private BranchServiceManager branchServiceManager;
	@Autowired
	private Organization organization;
		
	public String getNames()
	{
		User user = (User) ActionContext.getContext().getSession().get("user");
		int userId = user.getUserId();
		
		JSONObject jsonObject = JSONObject.fromObject(inParma);
		String searchType = jsonObject.getString("type");
		String keywords = jsonObject.getString("keywords");
		
		page = page == 0 ? 1:page;
		
		PageInfo pageInfo = branchServiceManager.getBranchRoots(userId, searchType, keywords, page);
		
		jsonObject.clear();
		jsonObject.put("found", 0);
		
		if (pageInfo != null) 
		{			
			jsonObject.put("curPage", pageInfo.getCurrentPage());
			jsonObject.put("pageSize", pageInfo.getPerPageCount());
			jsonObject.put("totalRecs", pageInfo.getTotalCount());
			jsonObject.put("totalPages", pageInfo.getTotalPageCount());
			jsonObject.put("data", JSONArray.fromObject(pageInfo.getDataList()).toString());
			jsonObject.put("found", 1);
		}
		
		rtnMsg = jsonObject.toString();
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	public String getOrgInfo()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		int rootId = Integer.parseInt(jsonObj.getString("rootId"));
		
		String childrenIds = branchServiceManager.getChildren(rootId);
		childrenIds = childrenIds.substring(2);
		List<String> list = Arrays.asList(childrenIds.split(","));
		
		List<Integer> subIds = CommonUtil.toIntegerList(list);
		List<ZTreeInfo> orgInfos = branchServiceManager.getOrgInfo(subIds);
		
		jsonObj.clear();
		jsonObj.put("found", 0);
		
		if (orgInfos != null && orgInfos.size() > 0)
		{
			jsonObj.put("found", 1);
			jsonObj.put("data", JSONArray.fromObject(orgInfos).toString());
		}
		
		rtnMsg = jsonObj.toString();
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	public String addOrganization()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		
		try
		{
			int upOrgId = Integer.parseInt(jsonObj.getString("pId"));
			String orgName = jsonObj.getString("orgName");
			
			Organization upOrg = branchServiceManager.getOrganization(upOrgId);
			
			organization.setOrgName(orgName);
			organization.setUpOrgId(upOrgId);
			organization.setOrgType(upOrg.getOrgType());
			organization.setCreateDate(new Date());
			organization.setStatusDate(new Date());
			organization.setUpdateDate(new Date());
			
			branchServiceManager.addOrganiztion(organization);
			
			jsonObj.clear();
			jsonObj.put("ok", 1);
			
		}
		catch (Exception e) 
		{
			jsonObj.clear();
			jsonObj.put("ok", 0);
			e.printStackTrace();
		}
		finally
		{
			rtnMsg = jsonObj.toString();
		}
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	public String updateOrganization()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		
		try
		{
			int orgId = Integer.parseInt(jsonObj.getString("id"));
			String orgName = jsonObj.getString("orgName");
			
			organization = branchServiceManager.getOrganization(orgId);
			
			organization.setOrgName(orgName);
			organization.setUpdateDate(new Date());
			
			branchServiceManager.updateOrganiztion(organization);
			
			jsonObj.clear();
			jsonObj.put("ok", 1);
			
		}
		catch (Exception e) 
		{
			jsonObj.clear();
			jsonObj.put("ok", 0);
			e.printStackTrace();
		}
		finally
		{
			rtnMsg = jsonObj.toString();
		}
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	public String delOrganization()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		
		try
		{
			int orgId = Integer.parseInt(jsonObj.getString("id"));
			
			branchServiceManager.delOrganiztion(orgId);
			
			jsonObj.clear();
			jsonObj.put("ok", 1);
			
		}
		catch (Exception e) 
		{
			jsonObj.clear();
			jsonObj.put("ok", 0);
			e.printStackTrace();
		}
		finally
		{
			rtnMsg = jsonObj.toString();
		}
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	
}
